package com.dingyx.ps.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * 〈OSS相关工具〉
 * @author ZhangYang
 * @date 2020/12/30 15:30
 * @version 1.0.0
 */
@Component
public class AliOSSUtil {


	/*
	* xianniu-file 命名空间下文件夹约定：
	* accessory：事件附件文件，相关数据都保存在base_accessory表中
	* ...
	*/
	private final String BUCKET = "xianniu-file";

	@Resource
	private PropertyUtil propertyUtil;

	/**
	 * 〈阿里云OSS文件上传（xianniu-file存储空间）〉
	 * @param inputStream 输入流
	 * @param sectionPath 文件名前的目录，格式为“XX/XX”（前后都不能包含“/”）
	 * @param fileName 文件名称（仅为文件名，不可以“/”开头）
	 * @return java.lang.String 文件URL
	 * @author ZhangYang
	 * @date 2020/12/30 15:50
	 * @version 1.0.0
	 */
	public String uploadFile(InputStream inputStream, String sectionPath, String fileName){
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = propertyUtil.getProperties("ali.oss.endpoint");
		// accessKey请登录https://ak-console.aliyun.com/#/查看
		String accessKeyId = propertyUtil.getProperties("ali.oss.accessKeyId");
		String accessKeySecret = propertyUtil.getProperties("ali.oss.accessKeySecret");
		String quoteOssUrl = propertyUtil.getProperties("ali.oss." + BUCKET);

		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
		// 上传文件
		PutObjectResult putObjectResult = ossClient.putObject(BUCKET, sectionPath + "/" + fileName, inputStream);
		System.out.println(putObjectResult);
		// 关闭client
		ossClient.shutdown();
//		System.out.println(quoteOssUrl+sectionPath+"/"+fileName);
		return quoteOssUrl+sectionPath+"/"+fileName;
	}

	/**
	 * <阿里云OSS base64文件上传（xianniu-file存储空间）>
	 * @param ImgBase64 1
	 * @param sectionPath 2
	 * @param fileName 3
	 * @return java.lang.String
	 * @author ZhangYang
	 * @date 2021/3/1 15:47
	 * @version 1.0.0
	 */
	public String uploadFileBase64(String ImgBase64, String sectionPath, String fileName){
		return null;
	}



}
